# encoding="UFT-8"
import requests
from lxml import etree
from flask import Flask
from flask import request, render_template, jsonify

app = Flask(__name__, static_folder='E:\py\爬虫\电影\static')
app.config['JSON_AS_ASCII'] = False

HEADERS = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36",
    "Referer": "http://www.baiwanzy.com/"
}


def getUrl(url, data):
    # name = input("请输入你要搜索的影视: ")
    # data["wd"] = name
    response = requests.post(url, data, headers=HEADERS)
    text = response.text
    html = etree.HTML(text)
    if html == "":
        return "暂无搜索结果"
    BASEURL = "http://www.baiwanzy.com"
    faurl = []
    ftext = html.xpath("//div[@class='xing_vb']//span[@class='xing_vb4']")
    for t in ftext:
        titles = t.xpath("./a/text()")
        for title in titles:
            pass
        urls = t.xpath("./a/@href")
        for url in urls:
            pass
        faurls = {
            "biaoti": title,
            "wangzhi": BASEURL + url
        }
        faurl.append(faurls)
    moviess = song(faurl)
    print(faurl)
    return moviess


def song(faurl):
    movies = []
    for url in faurl:
        response = requests.get(url["wangzhi"], headers=HEADERS)
        text = response.text
        html = etree.HTML(text)
        imagers = html.xpath("//div[@class='vodImg']/img/@src")
        print("".join(imagers))
        bieming = html.xpath("//div[@class='vodinfobox']//li/text()")
        bies = []
        for bie in bieming:
            b = bie.strip()
            bies.append(b)
        info = html.xpath("//div[@class='vodinfobox']//span/text()")
        fos = []
        for i in info:
            fo = i.strip()
            fos.append(fo)
        mydict = dict(zip(bies, fos))

        urlsss = html.xpath("//div[@style='padding-left:10px;word-break: break-all; word-wrap:break-word;']/ul")[
            0].xpath("./li/text()")
        m3u8s = html.xpath("//div[@style='padding-left:10px;word-break: break-all; word-wrap:break-word;']/ul")[
            1].xpath("./li/text()")
        m3u8 = []
        for m in m3u8s:
            m3 = m.split("$")
            m = {
                "name": m3[0],
                "url": m3[1]
            }
            m3u8.append(m)
        yuns = []
        for u in urlsss:
            yun = u.split('$')
            y = {
                "name": yun[0],
                "url": yun[1]
            }
            yuns.append(y)
        move = {
            "haibao": "".join(imagers),
            "info": mydict,
            "yun": yuns,
            "m3u8": m3u8
        }
        movies.append(move)
    return movies


@app.route('/')
def serch():
    return render_template("serch.html")


@app.route('/info')
def main():
    value = request.args.get('s')
    url = "http://www.baiwanzy.com/index.php?m=vod-search"
    data = {"wd": value, "submit": "search"}
    s = getUrl(url, data)
    if not s:
        return render_template("erro.html")
    else:
        return render_template("info.html", s=s)


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8080,threaded=True)
